package mo.offer_special;

public class L073 {

    public int minEatingSpeed(int[] piles, int h) {
        int max = 0;
        int sum = 0;
        for (int pile : piles) {
            sum += pile;
            max = Math.max(max, pile);
        }
        int left = (sum % h == 0) ? (sum / h) : (sum / h + 1), right = max;
        while (left < right) {
            int mid = left + (right - left) / 2;
            int time = 0;
            for (int pile : piles) {
                time += (pile % mid == 0) ? (pile / mid) : (pile / mid + 1);
            }
            if (time > h) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }

}
